写出ADO.NET访问数据库的步骤,并用C#语言写出连接数据库的代码,求大神帮忙,谢谢啦~ 5
1个回答
展开全部
我们可以用 ADO.NET 写出并执行 T-SQL 语句来创建表:
private void CreateTableBtn_Click(object sender, System.EventArgs e)
{
// 打开连接
if (conn.State == ConnectionState.Open)
conn.Close();
ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=mydb;" +
"Data Source=localhost;";
conn.ConnectionString = ConnectionString;
conn.Open();
sql = "CREATE TABLE myTable" +
"(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
private void CreateTableBtn_Click(object sender, System.EventArgs e)
{
// 打开连接
if (conn.State == ConnectionState.Open)
conn.Close();
ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=mydb;" +
"Data Source=localhost;";
conn.ConnectionString = ConnectionString;
conn.Open();
sql = "CREATE TABLE myTable" +
"(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询