asp.net连接sql问题

来源:百度知道 编辑:UC知道 时间:2024/05/04 22:10:25
private static string MyConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\web\\xx\\App_Data\\xuxi.mdf;Integrated Security=True;User Instance=True";

protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
CommandObject();
}

private void CommandObject()
{
SqlConnection MyConnection = new SqlConnection();
string cmdText = "select * from user";
SqlCommand MyCommand = new SqlCommand(cmdText, MyConnection);
myconnection.ConnectionString = MyConnectionString;
myconnection.Open();
SqlDataReader recm = MyCommand.ExecuteReader();
int index = 0;
string output = "<table border=1>";
while (recm.Read())
{
output += "<tr><td width=30>"

string cmdText = "select * from user";
改成:
string cmdText = "select * from [user]";

user是Sql2005的关键字。

一楼对啊,有可能吧table名字改一下吧。另外用using吧myconnection 和 recm 套起来吧;别用string啦,stringbuilder很便宜的。

是的,就是user关键字的错误加上一个[user] 就好了。还有数字开头的表名或是字段名,一定要用[]括起来