[英]C# odbc connection to sql creating tables but dont see in SQL Server
I want to create tables in SQL Server in my program. 我想在程序中的SQL Server中创建表。 The code compiles and everything seems to be ok, but after I close it the tables don't appear in the SQL Server database. 代码已编译,一切似乎都正常,但是在我关闭它之后,表没有出现在SQL Server数据库中。
EDITED EDITED
Please help here is the code and the connection string: 请帮助,这里是代码和连接字符串:
connectionString = "Driver={Sql Server}; Server=baxu\\sqlexpress; Database = baza1;" + $"UID ={ username };PWD={ password };";
Code: 码:
try
{
using (OdbcCommand comm = new OdbcCommand())
{
comm.Connection = cnn;
comm.CommandText = cmdString;
comm.ExecuteNonQuery();
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
This here should work for you: 这在这里应该为您工作:
NB: If you dont use trusted connection - Then look at the different connectionstrings here : 注意:如果您不使用受信任的连接-请在此处查看不同的连接字符串 :
var conn = new OdbcConnection();
conn.ConnectionString =
@"Driver={SQL Server};" +
@"Server=EGC25199\SQL2016;" +
@"DataBase=LegOgSpass;" +
@"Trusted_Connection=Yes;";
try
{
string cmdString = "CREATE TABLE dbo.odbctable (Wartosc int, Czas datetime)";
conn.Open();
using (OdbcCommand cmd = new OdbcCommand(cmdString, conn))
{
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
This might help. 这可能会有所帮助。 I noticed you used an unassigned name
in the create
command. 我注意到您在create
命令中使用了未分配的name
。
using System;
using System.Data;
using System.Data.Odbc;
class CommandOdbcExample{
static void Main() {
OdbcConnection comm = new OdbcConnection(@"DSN=MyOdbcdDB");
OdbcCommand nonqueryCommand = comm.CreateCommand();
try {
comm.Open();
nonqueryCommand.CommandText = "CREATE TABLE MyTable (Wartosc int, Czas datetime)";
Console.WriteLine(nonqueryCommand.CommandText);
nonqueryCommand.ExecuteNonQuery();
}
catch (OdbcException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
comm.Close();
Console.WriteLine("Connection Closed.");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.