首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

向数据库添加数据时发生的异常

2012-01-03 
向数据库添加数据时发生的错误protectedvoidButton1_Click1(objectsender,EventArgse){stringstrsqlinse

向数据库添加数据时发生的错误
protected   void   Button1_Click1(object   sender,   EventArgs   e)
        {
string   strsql   =   "insert   into   jobs   values( ' "   +   TextBox1.Text   +   " ', ' "   +   TextBox2.Text   +   " ', ' "   +   TextBox3.Text   +   " ', ' "   +   TextBox4.Text   +   " ') ";
ExcuteSql(strsql);
         

        }
        protected   void   ExcuteSql(string   strsql)
        {
                SqlConnection   con   =   new   SqlConnection( "Data   Source=.;Initial   Catalog=pubs;User   ID=sa;Password=wwwtqbbacom ");
                con.Open();
                SqlCommand   com   =   new   SqlCommand(strsql,   con);
                com.ExecuteNonQuery();
                con.Close();

        }
能够生成页面.按BUTTON按钮后就出现
仅当使用了列的列表,并且   IDENTITY_INSERT   为   ON   时,才能在表   'jobs '   中为标识列指定显式值。


[解决办法]
仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'jobs ' 中为标识列指定显式值。
===================
很明显,你对标识列插值了..估计你那个TextBox1.Text是不需要的
[解决办法]
不了解,是不是你 给标志列赋值了
[解决办法]
insert into jobs(对应的字段) values
[解决办法]
比较大的可能性

string strsql = "insert into jobs values( ' " + TextBox1.Text + " ', ' " + TextBox2.Text + " ', ' " + TextBox3.Text + " ', ' " + TextBox4.Text + " ') ";
========================================================
string strsql = "insert into jobs values( ' " + TextBox2.Text + " ', ' " + TextBox3.Text + " ', ' " + TextBox4.Text + " ') ";

热点排行