.NET新手系列(二)

接上次,继续我的菜鸟之旅,还请大家多少提点学习意见,呵呵。
 

1遇到的问题:
关于Cmd.ExecuteScalar()
解决方法:
这是一条返回单一值而不是数据行的命令,多数用于使用了countmaxmin等运算的select语句中。
示例:
SqlCommand Cmd = new SqlCommand(“SELECT count(*) FROM tablename;”);
int icount = (int) Cmd.ExecuteScalar();
另:该方法返回的是object类型,最好作转换
 

2遇到的问题:
关于commandtype
解决方法:
CommndaType SQL命令的类型
Text:就是最常用的SQL命令;
StoredProcedure:即存储过程;
TableDirect:指示SqlCommandCommandText的值为表名,打开此表并返回所有行和列
示例:
SqlCommand cmd=new SqlCommand("Users",sqlConn);
cmd.CommandType=CommandType.TableDirect
 

3遇到的问题:
如何修改asp.netweb应用程序的默认保存路径?
解决方法:
可以在internet信息服务管理器中。右键单击“默认WEB站点”属性,然后在“主目录”中修改即可。
 

4遇到的问题:
关于sqlserver数据库中的布尔型
解决方法:
sqlserver中布尔型可用:bit,值只取01,但它接受01之外的数据,但都作为1存储,且不允许空值。
 

5遇到的问题:
net中连接sqlserver:出现错误:* Error while accessing data,用户 ...ASPNET 登录失败。
解决方法:
Windows验证连接时,不必指定一个用户ID及口令,连接验证使用Windows NT2000的组帐号(group account)。SQL Server从信任连接属性中获取用户的帐号信息,将其与Windows已定义的帐号信息匹配和分析,如果正确就连接成功,并将此Windows帐号作为连接至SQL Server 2000的用户ID

如果连接字中包含有:Trusted_Connection=yes;
这就意味着连接将采用信任连接方式,但由于连接前没有用Windows组帐号(ASP环境中是访问IIS服务帐号IUSR_计算机名,在ASP.NET环境中帐号是ASPNET)登录至SQL Server 2000服务器, 也就是说没有建立一个信任连接(Trusted connection),当然,SQL Server 2000连接也不能够成功。

Trusted_Connection=yes;删除或改为Trusted_Connection=no;
这将不采用信任连接方式(也即不采用Windows验证方式),而改由SQL Server 2000验证方式,即在连接字中指定:User ID=user name;Password=user password;
SQL Server 2000会将此用户ID和口令进行验证连接,而与Windows帐号无关。 

另:
在提示aspnet用户登录失败时,也可以近按以下设置设置ASPNET账号:
sql企业管理器-〉服务器-〉安全性-〉登录-〉按下鼠标右键-〉新建登录-〉在名称中写入:ComputerName\ASPNET-〉数据库访问标签-〉在需要的数据库名字前面打勾-〉数据库角色中-〉选择db_owner-〉确定
 

6遇到的问题:
关于sql连接语句中的Integrated Security=SSPI
解决方法:
即:Security Support Provider Interface
设置Integrated Security True 的时候,连接语句前面的 UserID, PW 是不起作用的,即采用windows身份验证模式。
只有设置为 False 或省略该项的时候,才按照 UserID, PW 来连接。
Integrated Security 可以设置为: True, false, yes, no ,这四个的意思很明白了,还可以设置为:sspi ,相当于 True,建议用这个代替 True。
 

7遇到的问题:
Sqlserver中使用递增列
解决方法:
欲使用递增列,可以在建表后在企业管理器中右击表,选择设计表,点相应字段,在下方选用标识
 

8遇到的问题:
在后台代码中为某些控件的width属性赋值
解决方法:
设置width时,由于某些属性默认采用的单位是webcontrols.unit,而不是int,
可用Pixel方法:
imgtemp.Width = Unit.Pixel(100)
 

9遇到的问题:
图片存入数据库的简单代码
解决方法:
1)上载时选择本地文件的实现可能用现成的控件,即:
<inputid="upfile"type="file"runat="server">

2)上载的提交按钮的事件如下:

Dim Conn As New SqlClient.SqlConnection
Try
    'sql连接语句
    Dim strConn As String = ……
    Conn.ConnectionString = strConn
    Dim Cmd As New SqlClient.SqlCommand
    Cmd.Connection = Conn
    Dim intImageSize As Integer = upfile.PostedFile.ContentLength
    Dim strImageType As String = upfile.PostedFile.ContentType

    '将图片读入byte数组
    Dim imageStream As System.IO.Stream = upfile.PostedFile.InputStream
    Dim byteArray(intImageSize) As Byte
    imageStream.Read(byteArray, 0, intImageSize)

    '定义sql命令
    Dim strSql As String = "insert into myimage(photo) values (@photo)"

    'sql命令添加参数
    Cmd.CommandText = strSql
    Cmd.Parameters.Add("@photo", SqlDbType.Binary, intImageSize).Value = byteArray
    Conn.Open()
    Cmd.ExecuteNonQuery()
    Me.Label1.Text = "操作成功"
Catch ex As Exception
    Me.Label1.Text = ex.Message
End Try

Conn.Close()
(希望大家提供意见)
 

0遇到的问题:
关于使用存储过程
解决方法:
可在查询分析器中运行下列语句,创建存储过程:
create proc sp_getname
(@id char)
as
select name from proctest where id=@id

======================================================================

asp.net页面中编写:
Dim strConn As String = ……
Dim Conn As New SqlClient.SqlConnection
Conn.ConnectionString = strConn
Dim Cmd As New SqlClient.SqlCommand("sp_getname", Conn)
Cmd.CommandType = CommandType.StoredProcedure
Dim DR As SqlClient.SqlDataReader
Dim para As New SqlClient.SqlParameter("@id", "2")
Cmd.Parameters.Add(para)
Conn.Open()
DR = Cmd.ExecuteReader

If DR.Read Then
    Response.Write(DR(0))
End If

posted @ 2005-10-17 09:33  后厂村思维导图馆  阅读(1228)  评论(1编辑  收藏  举报