0

I have a basic form with an 'Add Recipe' button which is meant to write the following to me SQL Server Express database:

  1. Recipe Name
  2. Recipe Ingredients
  3. Recipe Instructions
  4. Recipe Image

The form is located at the bottom of the question and when I click the 'Add Recipe' button it makes a call to the updatedate() method, this method looks like so:

private void updatedata()
{ 
        // filestream object to read the image
        // full length of image to a byte array
        try
        {
                // try to see if the image has a valid path
                if (imagename != "")
                {
                    FileStream fs;
                    fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);

                    // a byte array to read the image
                    byte[] picbyte = new byte[fs.Length];
                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();

                    //open the database using odp.net and insert the lines
                    string connstr = @"Data Source=.;Initial Catalog=RecipeOrganiser;Persist Security Info=True;User ID=sa";

                    SqlConnection conn = new SqlConnection(connstr);
                    conn.Open();
                    string query;
                    query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) values (" + textBox1.Text + "," + " @pic" + "," + textBox2.Text + "," + textBox3.Text + ")";

                    SqlParameter picparameter = new SqlParameter();
                    picparameter.SqlDbType = SqlDbType.Image;
                    picparameter.ParameterName = "pic";
                    picparameter.Value = picbyte;
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.Parameters.Add(picparameter);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Image successfully saved");
                    cmd.Dispose();
                    conn.Close();
                    conn.Dispose();
                    Connection();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

My problem is when ever I click the 'Add recipe' button with the information populated, it freezes for around 30 seconds and then displays the following error, I have checked that the SQL Server services are running which thy are. Can anyone offer ay advice what I am doing wrong here?

enter image description here

Image of Form

enter image description here

4

3 回答 3

1
 string connstr = @"Data Source=.;Initial Catalog=RecipeOrganiser;Persist Security Info=True;User ID=sa";

需要是

 string connstr = @"Server=localhost\{SERVER_NAME};Database=RecipeOrganiser;Trusted_Connection=True;";

您可以从属性值“名称”用户 SQL 服务器属性中获取 SERVER_NAME

于 2013-10-20T13:33:42.620 回答
0

您的连接字符串对我来说看起来不太好...尝试使用 .Net SqlConnectionStringBuilder 类创建有效的连接字符串...设置 DataSource、InitialCatalog 和 IntegratedSecurity 或 UserId 和 Password 属性

于 2013-10-29T14:21:02.890 回答
0

使用以下链接

 SqlConnection con = new SqlConnection("Data Source=servername\\SQLEXPRESS;Initial Catalog=databasename;User ID=sa;Password=yoursqlserverpassword");

希望对你有帮助

于 2013-10-20T13:39:34.920 回答