数据未保存到数据库

时间:2019-01-29 08:20:10

标签: c# database

我正在为我的工作开发程序,但我找不到如何正确保存到数据库的方法。我已经尝试了所有我认为可能会出现问题的方法,但是没有任何效果。我有一些XAML UI,它的代码很长,所以我不会在这里放置。我的问题是我要保存到此数据库中:

CREATE TABLE [dbo].[checklist] (
[Id]            INT        IDENTITY (1, 1) NOT NULL,
[tb_00]         NCHAR (10) NULL,
[tb_01]         NCHAR (10) NULL,
[tb_02]         NCHAR (10) NULL,
[tb_03]         NCHAR (10) NULL,
[tb_04]         NCHAR (10) NULL,
[dp_00]         DATE       NULL,
[cb_A0]         NCHAR (10) NULL,
[cb_A1]         NCHAR (10) NULL,
[cb_A2]         NCHAR (10) NULL,
[cb_A3]         NCHAR (10) NULL,
[cb_A4]         NCHAR (10) NULL,
[cb_A5]         NCHAR (10) NULL,
[cb_A6]         NCHAR (10) NULL,
[cb_A7]         NCHAR (10) NULL,
[cb_A8]         NCHAR (10) NULL,
[cb_A9]         NCHAR (10) NULL,
[cb_A10]        NCHAR (10) NULL,
[cb_A11]        NCHAR (10) NULL,
[cb_A12]        NCHAR (10) NULL,
[cb_A13]        NCHAR (10) NULL,
[cb_A14]        NCHAR (10) NULL,
[cb_A15]        NCHAR (10) NULL,
[cb_A16]        NCHAR (10) NULL,
[cb_A17]        NCHAR (10) NULL,
[cb_A18]        NCHAR (10) NULL,
[cb_A19]        NCHAR (10) NULL,
[cb_A20]        NCHAR (10) NULL,
[cb_A21]        NCHAR (10) NULL,
[cb_A22]        NCHAR (10) NULL,
[cb_A23]        NCHAR (10) NULL,
[cb_A24]        NCHAR (10) NULL,
[cb_A25]        NCHAR (10) NULL,
[cb_A26]        NCHAR (10) NULL,
[cb_A27]        NCHAR (10) NULL,
[cb_A28]        NCHAR (10) NULL,
[cb_A29]        NCHAR (10) NULL,
[cb_A30]        NCHAR (10) NULL,
[cb_A31]        NCHAR (10) NULL,
[cb_A32]        NCHAR (10) NULL,
[cb_A33]        NCHAR (10) NULL,
[cb_A34]        NCHAR (10) NULL,
[cb_A35]        NCHAR (10) NULL,
[cb_A36]        NCHAR (10) NULL,
[cb_A37]        NCHAR (10) NULL,
[cb_A38]        NCHAR (10) NULL,
[cb_A39]        NCHAR (10) NULL,
[cb_A40]        NCHAR (10) NULL,
[cb_A41]        NCHAR (10) NULL,
[cb_A42]        NCHAR (10) NULL,
[cb_B0]         NCHAR (10) NULL,
[cb_B1]         NCHAR (10) NULL,
[cb_B2]         NCHAR (10) NULL,
[cb_B3]         NCHAR (10) NULL,
[cb_B4]         NCHAR (10) NULL,
[cb_B5]         NCHAR (10) NULL,
[cb_B6]         NCHAR (10) NULL,
[cb_B7]         NCHAR (10) NULL,
[cb_B8]         NCHAR (10) NULL,
[cb_B9]         NCHAR (10) NULL,
[cb_B10]        NCHAR (10) NULL,
[cb_B11]        NCHAR (10) NULL,
[cb_B12]        NCHAR (10) NULL,
[cb_B13]        NCHAR (10) NULL,
[cb_B14]        NCHAR (10) NULL,
[cb_B15]        NCHAR (10) NULL,
[cb_B16]        NCHAR (10) NULL,
[cb_B17]        NCHAR (10) NULL,
[cb_B18]        NCHAR (10) NULL,
[cb_B19]        NCHAR (10) NULL,
[cb_B20]        NCHAR (10) NULL,
[cb_B21]        NCHAR (10) NULL,
[cb_B22]        NCHAR (10) NULL,
[cb_B23]        NCHAR (10) NULL,
[cb_B24]        NCHAR (10) NULL,
[cb_B25]        NCHAR (10) NULL,
[cb_B26]        NCHAR (10) NULL,
[cb_B27]        NCHAR (10) NULL,
[cb_B28]        NCHAR (10) NULL,
[cb_B29]        NCHAR (10) NULL,
[cb_B30]        NCHAR (10) NULL,
[cb_B31]        NCHAR (10) NULL,
[cb_B32]        NCHAR (10) NULL,
[cb_B33]        NCHAR (10) NULL,
[cb_B34]        NCHAR (10) NULL,
[cb_B35]        NCHAR (10) NULL,
[cb_B36]        NCHAR (10) NULL,
[cb_B37]        NCHAR (10) NULL,
[cb_B38]        NCHAR (10) NULL,
[cb_B39]        NCHAR (10) NULL,
[canvas_podpis] IMAGE      NULL,
[tb_C0]         NCHAR (10) NULL,
[tb_C1]         NCHAR (10) NULL,
[tb_C2]         NCHAR (10) NULL,
[tb_C3]         NCHAR (10) NULL,
[tb_C4]         NCHAR (10) NULL,
[tb_C5]         NCHAR (10) NULL,
[tb_C6]         NCHAR (10) NULL,
[tb_C7]         NCHAR (10) NULL,
[tb_C8]         NCHAR (10) NULL,
[tb_C9]         NCHAR (10) NULL,
[tb_C10]        NCHAR (10) NULL,
[tb_C11]        NCHAR (10) NULL,
[tb_D0]         NCHAR (10) NULL,
[tb_D1]         NCHAR (10) NULL,
[tb_D2]         NCHAR (10) NULL,
[tb_D3]         NCHAR (10) NULL,
[tb_D4]         NCHAR (10) NULL,
[tb_D5]         NCHAR (10) NULL,
[tb_D6]         NCHAR (10) NULL,
[tb_D7]         NCHAR (10) NULL,
[tb_D8]         NCHAR (10) NULL,
[tb_D9]         NCHAR (10) NULL,
[tb_D10]        NCHAR (10) NULL,
[tb_D11]        NCHAR (10) NULL,
[tb_D12]        NCHAR (10) NULL,
[tb_D13]        NCHAR (10) NULL,
[tb_D14]        NCHAR (10) NULL,
[tb_D15]        NCHAR (10) NULL,
[tb_D16]        NCHAR (10) NULL,
[tb_E0]         NCHAR (10) NULL,
[tb_E1]         NCHAR (10) NULL,
[tb_E2]         NCHAR (10) NULL,
[tb_E3]         NCHAR (10) NULL,
[tb_E4]         NCHAR (10) NULL,
[tb_E5]         NCHAR (10) NULL,
[tb_E6]         NCHAR (10) NULL,
[tb_E7]         NCHAR (10) NULL,
[tb_E8]         NCHAR (10) NULL,
[tb_E9]         NCHAR (10) NULL,
[tb_E10]        NCHAR (10) NULL,
[tb_E11]        NCHAR (10) NULL,
[tb_E12]        NCHAR (10) NULL,
[tb_E13]        NCHAR (10) NULL,
[tb_E14]        NCHAR (10) NULL,
[tb_E15]        NCHAR (10) NULL,
[tb_E16]        NCHAR (10) NULL,
[tb_E17]        NCHAR (10) NULL,
[tb_E18]        NCHAR (10) NULL,
[tb_E19]        NCHAR (10) NULL,
[tb_E20]        NCHAR (10) NULL,
[tb_E21]        NCHAR (10) NULL,
[tb_E22]        NCHAR (10) NULL,
[tb_E23]        NCHAR (10) NULL,
[tb_F0]         NCHAR (10) NULL,
[tb_F1]         NCHAR (10) NULL,
[tb_F2]         NCHAR (10) NULL,
[tb_F3]         NCHAR (10) NULL,
[tb_F4]         NCHAR (10) NULL,
[tb_F5]         NCHAR (10) NULL,
[tb_F6]         NCHAR (10) NULL,
[tb_F7]         NCHAR (10) NULL,
[tb_F8]         NCHAR (10) NULL,
[tb_F9]         NCHAR (10) NULL,
[tb_F10]        NCHAR (10) NULL,
[tb_F11]        NCHAR (10) NULL,
[tb_G0]         NCHAR (10) NULL,
[tb_G1]         NCHAR (10) NULL,
[tb_G2]         NCHAR (10) NULL,
[tb_G3]         NCHAR (10) NULL,
[tb_G4]         NCHAR (10) NULL,
[tb_G5]         NCHAR (10) NULL,
[tb_G6]         NCHAR (10) NULL,
[tb_G7]         NCHAR (10) NULL,
[tb_G8]         NCHAR (10) NULL,
[tb_G9]         NCHAR (10) NULL,
[tb_G10]        NCHAR (10) NULL,
[tb_G11]        NCHAR (10) NULL,
[tb_H0]         NCHAR (10) NULL,
[tb_H1]         NCHAR (10) NULL,
[tb_H2]         NCHAR (10) NULL,
[tb_H3]         NCHAR (10) NULL,
[tb_H4]         NCHAR (10) NULL,
[tb_H5]         NCHAR (10) NULL,
[tb_H6]         NCHAR (10) NULL,
[tb_H7]         NCHAR (10) NULL,
[tb_H8]         NCHAR (10) NULL,
[tb_H9]         NCHAR (10) NULL,
[tb_H10]        NCHAR (10) NULL,
[tb_H11]        NCHAR (10) NULL,
[tb_pozn]       NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));

使用此:

 private void Uloz()
    {
        SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""|DataDirectory|\zldb.mdf"";Integrated Security=True");
        try
        {
            myConnection.Open();
            string sqlquery = "insert into checklist (tb_00,tb_01,tb_02,tb_03,tb_04,dp_00,cb_A0,cb_A1,cb_A2,cb_A3,cb_A4,cb_A5,cb_A6,cb_A7,cb_A8,cb_A9,cb_A10,cb_A11,cb_A12,cb_A13,cb_A14,cb_A15,cb_A16,cb_A17,cb_A18,cb_A19,cb_A20,cb_A21,cb_A22,cb_A23,cb_A24,cb_A25,cb_A26,cb_A27,cb_A28,cb_A29,cb_A30,cb_A31,cb_A32,cb_A33,cb_A34,cb_A35,cb_A36,cb_A37,cb_A38,cb_A39,cb_A40,cb_A41,cb_A42,cb_B0,cb_B1,cb_B2,cb_B3,cb_B4,cb_B5,cb_B6,cb_B7,cb_B8,cb_B9,cb_B10,cb_B11,cb_B12,cb_B13,cb_B14,cb_B15,cb_B16,cb_B17,cb_B18,cb_B19,cb_B20,cb_B21,cb_B22,cb_B23,cb_B24,cb_B25,cb_B26,cb_B27,cb_B28,cb_B29,cb_B30,cb_B31,cb_B32,cb_B33,cb_B34,cb_B35,cb_B36,cb_B37,cb_B38,cb_B39,canvas_podpis,tb_C0,tb_C1,tb_C2,tb_C3,tb_C4,tb_C5,tb_C6,tb_C7,tb_C8,tb_C9,tb_C10,tb_C11,tb_D0,tb_D1,tb_D2,tb_D3,tb_D4,tb_D5,tb_D6,tb_D7,tb_D8,tb_D9,tb_D10,tb_D11,tb_D12,tb_D13,tb_D14,tb_D15,tb_D16,tb_E0,tb_E1,tb_E2,tb_E3,tb_E4,tb_E5,tb_E6,tb_E7,tb_E8,tb_E9,tb_E10,tb_E11,tb_E12,tb_E13,tb_E14,tb_E15,tb_E16,tb_E17,tb_E18,tb_E19,tb_E20,tb_E21,tb_E22,tb_E23,tb_F0,tb_F1,tb_F2,tb_F3,tb_F4,tb_F5,tb_F6,tb_F7,tb_F8,tb_F9,tb_F10,tb_F11,tb_G0,tb_G1,tb_G2,tb_G3,tb_G4,tb_G5,tb_G6,tb_G7,tb_G8,tb_G9,tb_G10,tb_G11,tb_H0,tb_H1,tb_H2,tb_H3,tb_H4,tb_H5,tb_H6,tb_H7,tb_H8,tb_H9,tb_H10,tb_H11,tb_pozn) VALUES ('" + tb_00.Text + "','" + tb_01.Text + "','" + tb_02.Text + "','" + tb_03.Text + "','" + tb_04.Text + "','" + dp_00.SelectedDate + "','" + cb_A0.Text + "','" + cb_A1.Text + "','" + cb_A2.Text + "','" + cb_A3.Text + "','" + cb_A4.Text + "','" + cb_A5.Text + "','" + cb_A6.Text + "','" + cb_A7.Text + "','" + cb_A8.Text + "','" + cb_A9.Text + "','" + cb_A10.Text + "','" + cb_A11.Text + "','" + cb_A12.Text + "','" + cb_A13.Text + "','" + cb_A14.Text + "','" + cb_A15.Text + "','" + cb_A16.Text + "','" + cb_A17.Text + "','" + cb_A18.Text + "','" + cb_A19.Text + "','" + cb_A20.Text + "','" + cb_A21.Text + "','" + cb_A22.Text + "','" + cb_A23.Text + "','" + cb_A24.Text + "','" + cb_A25.Text + "','" + cb_A26.Text + "','" + cb_A27.Text + "','" + cb_A28.Text + "','" + cb_A29.Text + "','" + cb_A30.Text + "','" + cb_A31.Text + "','" + cb_A32.Text + "','" + cb_A33.Text + "','" + cb_A34.Text + "','" + cb_A35.Text + "','" + cb_A36.Text + "','" + cb_A37.Text + "','" + cb_A38.Text + "','" + cb_A39.Text + "','" + cb_A40.Text + "','" + cb_A41.Text + "','" + cb_A42.Text + "','" + cb_B0.Text + "','" + cb_B1.Text + "','" + cb_B2.Text + "','" + cb_B3.Text + "','" + cb_B4.Text + "','" + cb_B5.Text + "','" + cb_B6.Text + "','" + cb_B7.Text + "','" + cb_B8.Text + "','" + cb_B9.Text + "','" + cb_B10.Text + "','" + cb_B11.Text + "','" + cb_B12.Text + "','" + cb_B13.Text + "','" + cb_B14.Text + "','" + cb_B15.Text + "','" + cb_B16.Text + "','" + cb_B17.Text + "','" + cb_B18.Text + "','" + cb_B19.Text + "','" + cb_B20.Text + "','" + cb_B21.Text + "','" + cb_B22.Text + "','" + cb_B23.Text + "','" + cb_B24.Text + "','" + cb_B25.Text + "','" + cb_B26.Text + "','" + cb_B27.Text + "','" + cb_B28.Text + "','" + cb_B29.Text + "','" + cb_B30.Text + "','" + cb_B31.Text + "','" + cb_B32.Text + "','" + cb_B33.Text + "','" + cb_B34.Text + "','" + cb_B35.Text + "','" + cb_B36.Text + "','" + cb_B37.Text + "','" + cb_B38.Text + "','" + cb_B39.Text + "', @podpis_mech ,'" + tb_C0.Text + "','" + tb_C1.Text + "','" + tb_C2.Text + "','" + tb_C3.Text + "','" + tb_C4.Text + "','" + tb_C5.Text + "','" + tb_C6.Text + "','" + tb_C7.Text + "','" + tb_C8.Text + "','" + tb_C9.Text + "','" + tb_C10.Text + "','" + tb_C11.Text + "','" + tb_D0.Text + "','" + tb_D1.Text + "','" + tb_D2.Text + "','" + tb_D3.Text + "','" + tb_D4.Text + "','" + tb_D5.Text + "','" + tb_D6.Text + "','" + tb_D7.Text + "','" + tb_D8.Text + "','" + tb_D9.Text + "','" + tb_D10.Text + "','" + tb_D11.Text + "','" + tb_D12.Text + "','" + tb_D13.Text + "','" + tb_D14.Text + "','" + tb_D15.Text + "','" + tb_D16.Text + "','" + tb_E0.Text + "','" + tb_E1.Text + "','" + tb_E2.Text + "','" + tb_E3.Text + "','" + tb_E4.Text + "','" + tb_E5.Text + "','" + tb_E6.Text + "','" + tb_E7.Text + "','" + tb_E8.Text + "','" + tb_E9.Text + "','" + tb_E10.Text + "','" + tb_E11.Text + "','" + tb_E12.Text + "','" + tb_E13.Text + "','" + tb_E14.Text + "','" + tb_E15.Text + "','" + tb_E16.Text + "','" + tb_E17.Text + "','" + tb_E18.Text + "','" + tb_E19.Text + "','" + tb_E20.Text + "','" + tb_E21.Text + "','" + tb_E22.Text + "','" + tb_E23.Text + "','" + tb_F0.Text + "','" + tb_F1.Text + "','" + tb_F2.Text + "','" + tb_F3.Text + "','" + tb_F4.Text + "','" + tb_F5.Text + "','" + tb_F6.Text + "','" + tb_F7.Text + "','" + tb_F8.Text + "','" + tb_F9.Text + "','" + tb_F10.Text + "','" + tb_F11.Text + "','" + tb_G0.Text + "','" + tb_G1.Text + "','" + tb_G2.Text + "','" + tb_G3.Text + "','" + tb_G4.Text + "','" + tb_G5.Text + "','" + tb_G6.Text + "','" + tb_G7.Text + "','" + tb_G8.Text + "','" + tb_G9.Text + "','" + tb_G10.Text + "','" + tb_G11.Text + "','" + tb_H0.Text + "','" + tb_H1.Text + "','" + tb_H2.Text + "','" + tb_H3.Text + "','" + tb_H4.Text + "','" + tb_H5.Text + "','" + tb_H6.Text + "','" + tb_H7.Text + "','" + tb_H8.Text + "','" + tb_H9.Text + "','" + tb_H10.Text + "','" + tb_H11.Text + "','" + tb_pozn.Text + "');";
            string SqlDataPull = sqlquery;
            SqlCommand cmd = new SqlCommand(SqlDataPull, myConnection);

            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@podpis_mech", GlobalZL.podpis_mech);

        }
        catch
        {

        }
    }

当我查看数据库的数据时,只有一行充满了NULL。 谢谢您的回答。

1 个答案:

答案 0 :(得分:0)

我通过以下更改修改了您的代码:

  1. 使用块来确保SQL连接在使用后被清除
  2. 将用户输入作为参数传递以防止Sql注入
  3. 删除空的catch块,因为它吞没了抛出的所有错误
  4. 添加了cmd.ExecuteNonQuery();执行查询,该查询应将您的行插入数据库

P.S。我还删除了多余的列以使其可读

     private void Uloz()
        {
            using(SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""|DataDirectory|\zldb.mdf"";Integrated Security=True")){
                myConnection.Open();
                string sqlquery = @"insert into checklist (tb_00,tb_01) 
VALUES (@tb_00, @tb_01);";
                SqlCommand cmd = new SqlCommand(sqlquery , myConnection);    
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@podpis_mech", GlobalZL.podpis_mech);

//edit
//Pass parameters with explicit DB types
                    cmd.Parameters.Add(new SqlParameter("@tb_00", SqlDbType.NChar, 10) {Value = tb_00.Text});
                    cmd.Parameters.Add(new SqlParameter("@tb_01", SqlDbType.NChar, 10) {Value = tb_01.Text});
                    cmd.ExecuteNonQuery();
            }
        }