无法INSERT INTO表 [英] Can't INSERT INTO table

查看:109
本文介绍了无法INSERT INTO表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都在努力,但尽管没有任何错误,我根本无法在桌面上插入任何东西。我不知道代码的真正错误,因为我发送的数据是正确的,除非我的sintax中缺少某些东西。



它只是执行代码但不会插入新行。用于例如SELECT工作的表单中的其他函数就可以了。



I''ve been trying all day, but despite there being no error, I can''t insert anything at all to my table. I don''t know what is honestly wrong with the code, since the data I''m sending is correct, unless there is something missing in my sintax.

It just executes the code but won''t insert the new row. The other functions in the form that use for example SELECT work just fine.

public static string constr = @"Data Source=.\SQLEXPRESS;
                        AttachDbFilename=|DataDirectory|\cybercafe.mdf;
                        Integrated Security=True;User Instance=True;"; //in another form

public SqlConnection myConnection = new SqlConnection(MainContainer.constr);//catches the connection from the other form

string sqlIns = "INSERT INTO [Programs] (CodProgram, directory, logo ,description ,class ,type) VALUES (@codprogram,@directory,@logo,@description,@class,@type)";
myConnection.Open();
try
{
SqlCommand cmdIns = new SqlCommand(sqlIns, myConnection);
cmdIns.Parameters.AddWithValue("@codprogram",idprogram);
cmdIns.Parameters.AddWithValue("@directory", "'shortcuts\\" +txtname.Text +".lnk'");
cmdIns.Parameters.AddWithValue("@logo", "'icons\\" + txtname.Text + ".jpg'");
cmdIns.Parameters.AddWithValue("@description", txtdescription.Text);
cmdIns.Parameters.AddWithValue("@class", class);
cmdIns.Parameters.AddWithValue("@type", type);
cmdIns.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString()); 
}
myConnection.Close();





先谢谢你们。



Thanks in advance guys.

推荐答案

<pre lang="cs">cmdIns.Parameters.AddWithValue("@directory", "''shortcuts\\" +txtname.Text +".lnk''");</pre><br />





如果我是你,我不会'在字符串的开头和结尾(快捷方式之前和lnk之后)包括引号('')。



If I were you, I wouldn''t include quotes ('') in the beginning and end of the string (before shortcuts and after lnk).


这篇关于无法INSERT INTO表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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