我想将数据存储在sqlserver中,并且能够使用此代码。现在我想检查表是否存在,而不是插入数据或创建新表并插入数据。所以,我需要帮助... thnku尝试将数据存储到sqlserver中
SqlConnection con = new SqlConnection("Data Source=PRAWAT; Initial Catalog=StudentData ;Integrated security=true; ");
string query = "insert into NewValidStudentData(StudentId,Name,Marks) values (@StudentId,@Name,@Marks);";
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand(query, con);
con.Open();
da.InsertCommand.Parameters.Clear();
da.InsertCommand.Parameters.AddWithValue("@StudentId", System.Data.SqlDbType.NVarChar).Value = value[0];
da.InsertCommand.Parameters.AddWithValue("@Name", System.Data.SqlDbType.NVarChar).Value = value[1];
da.InsertCommand.Parameters.AddWithValue("@Marks", System.Data.SqlDbType.NVarChar).Value = value[2];
da.InsertCommand.ExecuteNonQuery();
con.Close();
当你对没有这个表的数据库运行这个错误时,你看过错误吗? – JeffO
在try/catch/create表中换行。或者在应用程序中启动查询information_schema.tables,以确定是否需要应用任何迁移。或者阅读关于迁移的内容,这是许多框架的一个特性,它们的功能完全相同。 – MatthewMartin
更好地在存储过程中执行SQL。如果您需要对SQL进行更改,请修改存储的proc,而不是C#应用程序。 –