【问题标题】:Add path to SQL Server using c#使用 c# 添加到 SQL Server 的路径
【发布时间】:2016-03-08 06:46:50
【问题描述】:

我尝试从 C# 应用程序向 SQL Server 添加文件路径,但出现了这个异常:

“System.Data.SqlClient.SqlException”类型的未处理异常 发生在 System.Data.dll

附加信息:附近有不正确的语法 'C:\Users\Misfen\Desktop\image.jpg'.

private void button9_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "JPG FILES (*.jpg)|*.jpg|JPEG FILES (*.jpeg)|*.jpeg|PNG FILES (*.png)|*.png";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        string picpath = dlg.FileName.ToString();
        listBox2.Items.Add(picpath);

    }
}

private void button5_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "JPG FILES (*.jpg)|*.jpg|JPEG FILES (*.jpeg)|*.jpeg|PNG FILES (*.png)|*.png|PDF FILES (*.pdf)|*.pdf|DOC FILES (*.doc)|*.doc";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        string picpath = dlg.FileName.ToString();
        textBox4.Text = picpath;
    }
}

private void button6_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "JPG FILES (*.jpg)|*.jpg|JPEG FILES (*.jpeg)|*.jpeg|PNG FILES (*.png)|*.png|PDF FILES (*.pdf)|*.pdf|DOC FILES (*.doc)|*.doc";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        string picpath = dlg.FileName.ToString();
        textBox5.Text = picpath;
    }
}

private void button7_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "JPG FILES (*.jpg)|*.jpg|JPEG FILES (*.jpeg)|*.jpeg|PNG FILES (*.png)|*.png";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        string picpath = dlg.FileName.ToString();
        listBox1.Items.Add(picpath);
    }    
}

C#代码:

String req = "insert into dommage_materiel values(" + textBox1.Text + ",'" + richTextBox2.Text + "','" + richTextBox1.Text + "','" + textBox3.Text + "','" + dateTimePicker1.Value + "','" + richTextBox3.Text + "','" + textBox2.Text + "','" + textBox4.Text.ToString() + "','" + textBox5.Text.ToString() + "'";

SqlCommand cmd = new SqlCommand(req, cnx);

cnx.Open();
cmd.ExecuteNonQuery();
cnx.Close();

for (int i = 0; i < listBox1.Items.Count; i++)
{
    SqlCommand cmd2 = new SqlCommand("insert into photo_domage values ('"+textBox1.Text+"','"+listBox1.Items[i].ToString()+"')", cnx);

    cnx.Open();
    cmd2.ExecuteNonQuery();
    cnx.Close();
}

for (int i = 0; i < listBox2.Items.Count; i++)
{
    SqlCommand cmd2 = new SqlCommand("insert into fichier_domage values ('" + textBox1.Text + "','" + listBox1.Items[i].ToString() + "')", cnx);

    cnx.Open();
    cmd2.ExecuteNonQuery();
    cnx.Close();
}

SQL 代码:

create table dommage_materiel
(
     num varchar(30) primary key,
     object_sistre varchar(500),
     discription varchar(1500),
     lieux varchar(100), 
     date_inci date, 
     domage varchar(50), 
     estimation varchar(30), 
     pv nvarchar(150), 
     facture nvarchar(1500)
)

create table photo_domage
(
     num varchar(30) foreign key references dommage_materiel (num), 
     ref nvarchar(1500)
)

create table fichier_domage 
(
     num varchar(30) foreign key references dommage_materiel (num),
     ref nvarchar(1500)
)

【问题讨论】:

标签: c# sql sql-server ado.net


【解决方案1】:

您忘记了第一个值周围的两个单引号,并且还缺少一个右括号。

只需替换

insert into dommage_materiel values(" + textBox1.Text + ",

insert into dommage_materiel values('" + textBox1.Text + "', .... ");

无论如何,我强烈建议您使用 Sql 参数。 Here 是一个很好且简短的解释。

【讨论】:

  • 是的,谢谢,我没有注意到,但同样的问题仍然存在
  • 最后还缺少一个右括号。
  • 好的,我已经更新了我的答案。不客气:-)
  • 如果对您有帮助,请将其标记为已回答以结束问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多