简体   繁体   English

C#SQL客户端:ALTER Table不起作用

[英]C# Sql Client: ALTER Table doesn't work

I have a code that is used to add some lines to SQL but it won't work. 我有一个用于向SQL添加一些行的代码,但是它不起作用。 Select and Delete are working but not the ALTER TABLE command. SelectDelete有效,但ALTER TABLE命令无效。

If I copy and paste just my console output into Microsoft Management Sql Query , it works. 如果我仅将控制台输出复制并粘贴到Microsoft Management Sql Query ,那么它将起作用。 ( tmp1 gets filled with some Name, tmp2 gets filled for Example CHAR(50)) tmp1被填充了一些名称, tmp2被填充了示例CHAR(50))

Edit: I dont get any Error, in the logs of my SQL server i dont see any command called "Alter" to be excuted. 编辑:我没有得到任何错误,在我的SQL Server的日志中,我没有看到被称为“ Alter”的任何命令。

 string tmp1, tmp2;
 tmp1 = addfrm.getTableName();
 tmp2 = addfrm.getType();

 string constring = @"Data Source=" + adr + ";Initial Catalog=" + dat + ";User ID=" + user + ";Password=" + pwd;
 try
 {
     using (SqlConnection con = new SqlConnection(constring))
     {
         string tmp = @"ALTER TABLE " + tbl + " ADD " + tmp1 + " " + tmp2;
         Console.WriteLine("Mein Befehl lautet: " + tmp);

         using (SqlCommand cmd = new SqlCommand(tmp, con))
         {
             cmd.CommandType = CommandType.Text;
             using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
             {
             }
         }
     }
 }

 catch (SqlException) 
 { 
     MessageBox.Show("Fehler"); 
 }

You don't send SQL query to database. 您不将SQL查询发送到数据库。 Use ExecuteNonQuery on SqlCommand. 在SqlCommand上使用ExecuteNonQuery。 Instead of: 代替:

using (SqlCommand cmd = new SqlCommand(tmp, con))
{
    cmd.CommandType = CommandType.Text;
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {

    }
}

use 采用

using (SqlCommand cmd = new SqlCommand(tmp, con))
{
    cmd.ExecuteNonQuery();
}

Adapter will not execute query until adapter.Fill is called. 直到adapter.Fill被调用,适配器才会执行查询。

You are not setting the values properly. 您没有正确设置值。 Try this it should work. 试试这个,它应该工作。

string tmp1, tmp2;
                tmp1 = addfrm.getTableName();
                tmp2 = addfrm.getType();

                string constring = @"Data Source=" + adr + ";Initial Catalog=" + dat + ";User ID=" + user + ";Password=" + pwd;
                try
                {
                    using (SqlConnection con = new SqlConnection(constring))
                    {
                        string tmp = @"UPDATE TABLE " + tbl + " SET Col1 = '" + temp1 +"',Col2='" + tmp2 +"'";

                        Console.WriteLine("Mein Befehl lautet: " + tmp);

                        using (SqlCommand cmd = new SqlCommand(tmp, con))
                        {
                            cmd.CommandType = CommandType.Text;
                            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                            {
    cmd.ExecuteNonQuery();
                            }
                        }
                    }
                }
                catch (SqlException) { MessageBox.Show("Fehler"); }

i just avoided it now with Fill. 我现在用Fill避免了它。 Its bit messy but ok. 有点混乱,但还可以。 Thanks to Pawel who said it. 感谢Pawel所说的。

                            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                        {
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM