且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Visual Studio 2008中更新SQL数据库的问题

更新时间:2023-02-08 17:34:30

首先发生的事情是我是你将IDMax设置为不是ID列的最高值,而是设置为数据库中的行数。



不会选择MAX(ID)从表更准确? (甚至更好,在Db中有一个自动增量Id字段,所以你不必自己设置它)



如果我有这个问题我会停止在更新行的调试和检查Id的值(我也会检查我的列名称是否真的是Discription而不是说明) - 甚至可能将它更改为你知道不在Db中的值>


还要检查你要插入的行的rowstate。
First thing that occurs to me is that you are setting IDMax not to the highest value of the ID column, but to the number of rows in the database.

Would not select MAX(ID) from table be more accurate? (even better, have an auto increment Id field in the Db so you don''t have to set it yourself)

If I had this issue I would be stopping in debug on the update line and checking the value of Id (I''d also check that my column name really was Discription and not Description) - maybe even change it to a value you know isn''t in the Db

Also check the rowstate of the row you are trying to insert.


当一个单元格的数据在其中时,这是我的Datagridview已更改。



this is my Datagridview when one cell''s data in it is changed.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!boolAdd)
            {
                if (e.RowIndex != -1)
                {
                    var db = new LINQDataContext();
                    int intCurrentRow = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["Row"].Value.ToString());
                    try
                    {
                        string strYear = dataGridView1.Rows[e.RowIndex].Cells["Year"].Value.ToString();
                        string strMonth = dataGridView1.Rows[e.RowIndex].Cells["Month"].Value.ToString();
                        string strDay = dataGridView1.Rows[e.RowIndex].Cells["Day"].Value.ToString();
                        // MessageBox.Show(strDay);
                        string strHour = dataGridView1.Rows[e.RowIndex].Cells["Hour"].Value.ToString();
                        string strMinute = dataGridView1.Rows[e.RowIndex].Cells["Minute"].Value.ToString();
                        string strSecond = dataGridView1.Rows[e.RowIndex].Cells["Second"].Value.ToString();
                        string strRepetition = dataGridView1.Rows[e.RowIndex].Cells["Repetition"].Value.ToString();
                        string strX = dataGridView1.Rows[e.RowIndex].Cells["X"].Value.ToString();
                        string strY = dataGridView1.Rows[e.RowIndex].Cells["Y"].Value.ToString();
                        string strZ = dataGridView1.Rows[e.RowIndex].Cells["Z"].Value.ToString();
                        string strOperator = dataGridView1.Rows[e.RowIndex].Cells["Operator"].Value.ToString();
                        db.Updatetable(intCurrentRow, strYear, strMonth, strDay, strHour, strMinute, strSecond, strRepetition, strX, strY, strZ, strOperator);
                        LINQDataContext dbo = new LINQDataContext();
                        dataGridView1.DataSource = dbo.mytbls;   //Refresh Grid After Edit
                    }
                    catch { }

                }
            }
        }







我的更新程序:






My Update Procedure:

CREATE PROCEDURE dbo.Update1 @Row bigint, @Word nvarchar(50), @Mean1 nvarchar(50), @Mean2 nvarchar(50), @Mean3 nvarchar(50), @Mean4 nvarchar(50), @Example1 nvarchar(MAX), @Example2 nvarchar(MAX), @Example3 nvarchar(MAX), @Example4 nvarchar(MAX), @WordExist nvarchar(10), @Mean1Exist nvarchar(10), @Mean2Exist nvarchar(10), @Mean3Exist nvarchar(10), @Mean4Exist nvarchar(10), @Example1Exist nvarchar(10), @Example2Exist nvarchar(10), @Example3Exist nvarchar(10), @Example4Exist nvarchar(10)
AS
UPDATE info SET Row=@Row , Word=@Word , Mean1=@Mean1 , Mean2=@Mean2 , Mean3=@Mean3 , Mean4=@Mean4 , Example1=@Example1 , Example2=@Example2 , Example3=@Example3 , Example4 =@Example4 ,WordExist=@WordExist, Mean1Exist=@Mean1Exist ,Mean2Exist =@Mean2Exist ,Mean3Exist= @Mean3Exist ,Mean4Exist= @Mean4Exist ,Example1Exist= @Example1Exist ,Example2Exist= @Example2Exist , Example3Exist=@Example3Exist ,Example4Exist= @Example4Exist
WHERE Row=@Row
RETURN





按字段更新字段的另一种方法!







Another way for updating Field by field!


 var db = new MyMapDataDataContext();
var query = from ord2 in db.IsZoomTableFilledTables where ord2.Key == 1 select ord2;
           foreach (var ord2 in query)
           {
               ord2.IsZoomTableFilled6 = 1;
           }
           try
           {
               db.SubmitChanges();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           db.Dispose();


更改



string connectionStringGroup =Data Source = .\\SQLEXPRESS; AttachDbFilename = | DataDirectory | \\German.mdf; Integrated Security = True; User Instance = True;



to



string connectionStringGroup =Data Source = .\\SQLEXPRESS; AttachDbFilename = E:\\SynonymWorld \\SynonymWorld \\ \\\ German.mdf; Integrated Security = True; User Instance = True;





似乎DataDirectory导致数据库不能更新!!我不明白原因,但它解决了这个问题。
change

string connectionStringGroup = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\German.mdf;Integrated Security=True;User Instance=True";

to

string connectionStringGroup = "Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\SynonymWorld\\SynonymWorld\\German.mdf;Integrated Security=True;User Instance=True";


it seems DataDirectory causes the database not to be updated!! I do not understand the reason, but it solve the problem.