我已经为sql数据库创建了一个代码插入代码..但是当我运行该代码时它会出错 [英] I have made a code for image insertion to sql database.. but when i run that code it gives an error

查看:75
本文介绍了我已经为sql数据库创建了一个代码插入代码..但是当我运行该代码时它会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button2_Click(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";

        String Strt_Address = TextBox1.Text;
        String End_Address = TextBox2.Text;

        String filePath = FileUpload1.PostedFile.FileName;
        String filename = Path.GetFileName(filePath);
        String ExtStr = Path.GetExtension(filename);
        String contenttype = String.Empty;

        switch (ExtStr)
        { 
            case ".png":
                contenttype = "image/png";
                break;

            case ".jpg":
                contenttype = "image/jpg";
                break;

            case ".gif":
                contenttype = "image/gif";
                break;
        }

        if (contenttype != string.Empty)
        {
        Stream Strmf = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(Strmf);
            Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);
            
            conn.Open();

            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;
            scmd.ExecuteNonQuery();


        }







伙计指向



scmd.ExecuteNonQuery();并说



'Image'附近的语法不正确。





。 .................................................. 。



i有数据库值...

和我的sql数据库包括..



来源Varchar(最大)

年龄int

图像Varbinary(最大)



so在那里,我手动存储值



例如=姓名约翰,年龄20



i想要查看天气用户输入john或不输入文本框...



如果textbox值等于john ...那么只有我可以将图像添加到Image Field ..

//我需要比较文本框和数据库之间的值。

.....



想想Facebook的例子...我只能添加我的照片,如果我登录我的帐户只知道。



那么那么......






guys it points to

scmd.ExecuteNonQuery(); and says

Incorrect syntax near 'Image'.


....................................................

i have values on database...
and my sql database consist..

Source Varchar(max)
Age int
Image Varbinary(max)

so there, i store values manually

eg = name john , age 20

i want to check weather user input john or not to the textbox...

if textbox value equals to john ... then only i can add images to Image Field..
// i need to compare the values between textbox and database.
.....

Think example as Facebook... i can only add my pics if i'm login with my account only know.

so kind of that...

String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";





怎么样?



how about this?

推荐答案

查看您的查询:



Look at your query:

String QueryStr = "INSERT INTO MapDataImage VALUES Image WHERE @Source='" + TextBox1.Text + "';";
SqlCommand scmd = new SqlCommand(QueryStr);
scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
scmd.ExecuteNonQuery();

您指定变量 @Source 但仅为变量 @Image 可能你想说的是:



You are specifying the variable @Source but only providing a value for the variable @ImageProbably, what you wanted to say was:

String QueryStr = "INSERT INTO MapDataImage VALUES (@Source, @Image)";
SqlCommand scmd = new SqlCommand(QueryStr);
scmd.Parameters.AddWithValue("@Image", imgbytes);
scmd.Parameters.AddWithValue("@Source", TextBox1.Text);
scmd.ExecuteNonQuery();

但最好还要在查询中提供字段名称,以确保它们在正确的位置...

But it is a good idea to also provide the field names in your query to ensure they go in the right place...

INSERT INTO myTable (MyColumn1, MyColumn2) VALUES (@MC1, @MC2)


编辑此行

Edit this line
.....................
 Stream Strmf = FileUpload1.PostedFile.InputStream;
 BinaryReader br = new BinaryReader(Strmf);
 Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);            
 conn.Open(); 
 String QueryStr = "INSERT INTO MapDataImage VALUES Image WHERE @Source='" + TextBox1.Text + "';";
 SqlCommand scmd = new SqlCommand(QueryStr,your connection);//Connection required



问候......:笑:


Regards..:laugh:


这篇关于我已经为sql数据库创建了一个代码插入代码..但是当我运行该代码时它会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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