如何将值插入数据库中的表 [英] how to insert values to a table in the database

查看:82
本文介绍了如何将值插入数据库中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码不会引起任何错误.但是尚未插入值.消息成功插入"也未运行,代码有什么问题?有人可以帮忙吗?

the following code doesn''t cause for any error.but yet the values are not inserted.the message "Successfully Inserted" is also not running what is wrong with the code?can any one help me?

byte[] imagedata;
private int n = 0;
private string retVal;

private SqlConnection conn;
private SqlCommand com;
private SqlCommand SqlCom;
private SqlDataReader dr;

private void button3_Click(object sender, EventArgs e)
{
 if (textBox2.Text == "" || textBox3.Text == "")
   MessageBox.Show("Please make sure to fill the compulsary fields!!");
 else
  {
   textBox1.Visible = true;
   int no = generateCandNo();
   textBox1.Text = Convert.ToString(no);
    try
    {
    openConnection();
    string qry = "insert into candidates (cand_no, cand_name, party(name), party_im, name_im) values (@no, @nm,@pty,@ImageData1,@ImageData2)";

    SqlCom = new SqlCommand(qry, conn);
    SqlCom.Parameters.Add(new SqlParameter("@no",textBox1.Text));
    SqlCom.Parameters.Add(new SqlParameter("@nm", textBox2.Text));
    SqlCom.Parameters.Add(new SqlParameter("@pty", textBox3.Text));
    SqlCom.Parameters.Add(new SqlParameter("@ImageData1",(object)imagedata));
    SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));
    n = -1;
    n = SqlCom.ExecuteNonQuery();
    closeConnection();
    MessageBox.Show("Successfully Inserted");
    }
    catch (Exception ee)
    {string error = ee.Message;}
  }
}

private int generateCandNo()
{
 string query = "select cand_no from candidates where cand_no=(SELECT max(cand_no)FROM candidates);";
 openConnection();
 com = new SqlCommand(query, conn);
 dr = com.ExecuteReader();
 while (dr.Read())
 {  retVal = dr[0].ToString(); }
 com.Cancel();
 com.Dispose();
 closeConnection();
 int cand_no= Convert.ToInt32(retVal);
 textBox1.Text = retVal;
 cand_no++;
 return cand_no;
}



表定义



table definition

cand_no       	nvarchar(50)
cand_name       nvarchar(50)
party(name)	varchar(50)
party_im	image
name_im	        image



[edit]已添加代码块,忽略HTML ..."选项已禁用-OriginalGriff [/edit]



[edit]Code blocks added, "Ignore HTML..." option disabled - OriginalGriff[/edit]

推荐答案

我可能会猜测它是一方" (名称)"部分的SQL命令.要进行检查,请尝试做一些有用的例外操作:
I would hazard a guess that it is the "party(name)" part of the SQL command. To check, try doing something useful with the exception:
catch (Exception ee)
{string error = ee.Message;}

那只是把它扔掉了,并阻止了编译器抱怨-这可能就是您想要的!但是,这意味着您无法找出问题所在:尝试改用MessageBox.Show,或将错误记录到文件中.


有错误,
n = SqlCom.ExecuteNonQuery();
这有什么问题?"


而且我猜测错误是" party"不是公认的内置函数名称."?

如果您的列名为"party(name)",则必须将其括起来才能使用.如果不是,那么您需要使用列的名称!

That just throws it away and stops the compiler complaining - which was probably what you intended! However, it means that you can''t find out what is going wrong: Try a MessageBox.Show instead, or log the error to a file.


"error is in,
n = SqlCom.ExecuteNonQuery();
what is wrong in this?"


And I am guessing that the error is "''party'' is not a recognized built-in function name."?

If your column is called "party(name)" then it need to be enclosed to work. If it isn''t, then you need to use the name of the column!

string qry = "insert into candidates (cand_no, cand_name, [party(name)], party_im, name_im) values (@no, @nm,@pty,@ImageData1,@ImageData2)";

或:

string qry = "insert into candidates (cand_no, cand_name, party_name, party_im, name_im) values (@no, @nm,@pty,@ImageData1,@ImageData2)";


可以说您的数据库连接是A-OK并已建立,分配给您的对象的值是否在其中?你追踪到了吗?
我指的是这个:
lets say your database connection is A-OK and established, did the values assigned to your objects are in? have you trace it?
Im referring to this :
SqlCom.Parameters.Add(new SqlParameter("@no",textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("@nm", textBox2.Text));
SqlCom.Parameters.Add(new SqlParameter("@pty", textBox3.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData1",(object)imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));


您可能想在SqlParameters上设置类型,即:

You might want to set the types on the SqlParameters ie:

SqlCom.Parameters.Add(new SqlParameter("@no", System.Data.SqlDbType.NVarChar,50));
SqlCom.Parameters.Add(new SqlParameter("@nm", System.Data.SqlDbType.NVarChar,50));
SqlCom.Parameters.Add(new SqlParameter("@pty", System.Data.SqlDbType.Nvarchar,50));
SqlCom.Parameters.Add(new SqlParameter("@ImageData1",System.Data.SqlDbType.Image));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2",System.Data.SqlDbType.Image));

SqlCom.Paramters["@no"].Value = textBox1.Text;
...



字节数组可以转换为几件事(二进制,VarBinary,图像和时间戳),因此最好在可能的地方设置正确的类型.

另外,如上所述,party(name)列可能会引起问题,我将列名称包装在[]中,以确保不是这种情况.



There are a couple of things that byte arrays can be converted to (Binary, VarBinary, Image and timestamp) so its best to set the correct types where you can.

Also as mentioned above the party(name) column might be causing issues, I''d wrap the column names in [] to make sure that is no the case.

string qry = "insert into candidates ([cand_no], [cand_name], [party(name)], [party_im], [name_im]) values (@no, @nm,@pty,@ImageData1,@ImageData2)";


这篇关于如何将值插入数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆