English 中文(简体)
INSERT 报表不将数据插入表格
原标题:INSERT statement doesn t insert data into table

我对 INSTERT 语句有问题。 我从网格viev 获得数据, 效果很好, 但我不能把它们插入表格

private void button3_Click(object sender, EventArgs e)
{
    int amorplanid = 0;
    int idn = 0;
    DateTime datum;
    double interest = 0;
    double principal = 0;
    double payment = 0;
    double newprincipal = 0;

    string nizz = "";
    string[] niz= new string[7];
    for (int x = 0; x < dataGridView1.Rows.Count-1; x++)
    {
        for (int j = 0; j < dataGridView1.Rows[x].Cells.Count; j++)
        {
            nizz += dataGridView1.Rows[x].Cells[j].Value.ToString()+"."; 
        }
        niz = nizz.Split( . );

        amorplanid = System.Convert.ToInt32(niz[0]);
        idn = System.Convert.ToInt32(niz[1]);
        // datum = System.Convert.ToDateTime(niz[2]);
        datum = DateTime.Now;
        interest = System.Convert.ToDouble(niz[3]);
        principal = System.Convert.ToDouble(niz[4]);
        payment = System.Convert.ToDouble(niz[5]);
        newprincipal = System.Convert.ToDouble(niz[6]);

        String insert = @"INSERT INTO AmortPlanCoupT(ID, AmortPlanID, CoupDate, Interest, Principal, Payxment, NewPrincipal) VALUES (" + idn + "," + amorplanid + "," + datum + "," + (float)interest + "," + (float)principal + "," + (float)payment + "," + (float)newprincipal + ")";
        SqlConnection myconn = new SqlConnection(conn);
        // String MyString = @"INSERT INTO Employee(ID, FirstName, LastName) VALUES(2,  G ,  M )";
        try
        {
            myconn.Open();
            SqlCommand cmd = new SqlCommand(insert, myconn);
            cmd.ExecuteNonQuery();
            myconn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    label14.Text = niz[0];
}

我创建了一个 Windows 控制台应用程序来测试 :

我有表 test , 有两个列 id(int), leto(float) ;

SqlConnection MyConnection = new SqlConnection(@"Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|App_DataDatabase1.mdf;Integrated Security=True;User Instance=True");

try
{
    MyConnection.Open();

    String MyString = @"INSERT INTO test(id, leto) VALUES(2, 2)";
    SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);

    MyCmd.ExecuteScalar();
    MyConnection.Close();
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

我一直在尝试不同的东西 写数据到桌子上, 只是不能让他们在那里。

问题回答

正如我先前在此站点上说过的那样, 整个 < em> 用户事件和 AnntDbFileName\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

如果您想要坚持此方针, 请尝试在 < code> myConnection. close () call - 上设置一个断点, 然后用 SQL 服务器 Mgmt Studio Express 检查 < code>. mdf 文件 - 我几乎可以肯定您的数据存在 。

我认为,“强力”的真正解决办法(/强)是:

  1. 安装 SQL 服务器 Express (而您无论如何已经这样做了)

  2. 安装 SQL 服务器管理工作室 Express

  3. 创建您在 < enger> SSMS Express 中的数据库, 给它一个逻辑名称( 如 < code> Database1 ) 。

  4. 使用逻辑 < 坚固> 数据库名称 < / 坚固 > (在服务器上创建时设定) 连接到它 - 并且不要乱动物理数据库文件和用户实例。 在这种情况下, 您的连接字符串会像 :

    Data Source=.SQLEXPRESS;Database=Database1;Integrated Security=True
    

    其它东西都和以前一样 坚固...

在你继续前进之前,你应该改写你的代码来使用参数,而不是用字符串连接。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签