我将值插入数据库....但现在它不工作? [英] i insert values to database.... but now it's not working?

查看:62
本文介绍了我将值插入数据库....但现在它不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符串 selQuery =   SELECT Id FROM MapDataImage WHERE Source =' + Source_Box.Text +  'AND Destination = ' + Distance_Box.Text +  '; 
{
// 创建新的sql命令以通过它运行查询。
SqlCommand scmd = new SqlCommand(selQuery,conn2);

// open connection
conn2.Open();

// 创建一个sql数据读取器,通过sql命令读取查询
SqlDataReader sqldread = scmd.ExecuteReader();

// 直到sql数据读取器读取查询
< span class =code-keyword> while (sqldread.Read())
{
// 从sql mapImagedata表中检索id
int Dbid =( int )sqldread [ Id];
// string DbId = sqldread.GetInt32(Id)。ToString();

// 这将检查是否有任何有效ID
if (Dbid!= null
{
// 使用insert查询将具有特定id名称的图像添加到另一个sql数据表
// mapimagedata表的id将是主键,主键将是新图像表的foriegn键
字符串 QueryStr = INSERT INTO Add_Comment(Id, User_Comments)VALUES(' + Dbid + ',@ User_Comments);
scmd1 = new SqlCommand(QueryStr,conn2);

}
}
// 用于sql数据读取器的dispose命令
sqldread.Dispose();
// 将二进制数据添加到数据表中的特定数据字段
scmd1。 Parameters.AddWithValue( @ User_Comments,UserComment.Text);

// 使用执行执行所有命令,根据sqlcommand
scmd1.ExecuteNonQuery();
// String QueryStr =UPDATE MapDataImage SET Image = @ Image WHERE Source ='+ TextBox1.Text +';;
// SqlCommand scmd = new SqlCommand(QueryStr,conn) ;
// scmd.Parameters.Add(@ Image,SqlDbType.VarBinary) .Value = imgbytes;

conn2.Close();
// 连接关闭







这里发生了什么?



第一次查询根据来源和目的地选择id />


和第二个查询将值插入到具有第一个表ID的另一个表中



但现在它只是插入ID

不是UserComment?

为什么................

解决方案

0)使用参数化查询

1)如果ID是 int ,为什么要把它放在撇号中?

2)使用使用语句

3)不需要 if(Dbid!= null)



4)为什么不简单地使用 INSERT / SELECT 声明?



 INSERT INTO Add_Comment(Id,User_Comments)
SELECT ID,@ User_Comments
FROM MapDataImage
WHERE Source = @ Source AND Destination = @ Destination


String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + Source_Box.Text + "' AND Destination='" + Distance_Box.Text + "'";
        {
            // create new sql command to run the query through it.
            SqlCommand scmd = new SqlCommand(selQuery, conn2);

            // open connection
            conn2.Open();

            //create a sql data reader to read the query through sql command
            SqlDataReader sqldread = scmd.ExecuteReader();

            // untill sql data reader read the query
            while (sqldread.Read())
            {
                //retrieve id from the sql mapImagedata table
                int Dbid = (int)sqldread["Id"];
                //string DbId = sqldread.GetInt32("Id").ToString();

                // this will check is there any valid id or not
                if (Dbid != null)
                {
                    // used insert query to add image with particular id name to another sql data table
                    // id of the mapimagedata table will be primary key and that primary key will be the foriegn key of the new image table 
                    String QueryStr = "INSERT INTO Add_Comment(Id,User_Comments) VALUES ('" + Dbid + "',@User_Comments)";
                    scmd1 = new SqlCommand(QueryStr, conn2);
                    
                }
            }
            //dispose command for sql data reader
            sqldread.Dispose();
            // add binary data to particular datafield in the data table
            scmd1.Parameters.AddWithValue("@User_Comments", UserComment.Text);
            
            //using execution for execute all the commands according to sqlcommand 
            scmd1.ExecuteNonQuery();
            //String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";
            //SqlCommand scmd = new SqlCommand(QueryStr, conn);
            //scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;

            conn2.Close();
            //connection close




here what happens is ?

first query select the id according to source and destination

and second query insert values to another table with first table ID

but now it's insert ID only
not UserComment?
why is that................

解决方案

0) Use a parameterized query
1) If ID is an int, why put it in apostrophes?
2) Use using statements
3) No need for if (Dbid != null)

4) Why not simply use an INSERT/SELECT statement?

INSERT INTO Add_Comment(Id,User_Comments)
SELECT ID,@User_Comments
FROM MapDataImage 
WHERE Source=@Source AND Destination=@Destination


这篇关于我将值插入数据库....但现在它不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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