C#无法建立SQL Server连接

时间:2013-10-20 13:30:52

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

我有一个带有“添加配方”按钮的基本表单,用于将以下内容写入SQL Server Express数据库:

  1. 食谱名称
  2. 食谱成分
  3. 食谱说明
  4. 食谱图片
  5. 表单位于问题的底部,当我点击“添加配方”按钮时,它会调用updatedate()方法,此方法如下所示:

    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);
                }
            }
    

    我的问题是,当我点击“添加配方”按钮并填充了信息时,它会冻结约30秒然后显示以下错误,我已经检查过SQL Server服务正在运行您的身份。任何人都可以提出我在这里做错的建议吗?

    enter image description here

    表格形象

    enter image description here

3 个答案:

答案 0 :(得分: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;";

您可以从属性值“Name”用户获取SERVER_NAME SQL Server属性

答案 1 :(得分:0)

使用以下链接

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

希望它能帮到你

答案 2 :(得分:0)

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

相关问题