using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Chapter13
{
public partial class CommandText : Form
{
public CommandText()
{
InitializeComponent();
}
private void CommandText_Load(object sender, EventArgs e)
{
//create connection
SqlConnection conn = new SqlConnection(@"server = .\sqlexpress;
integrated security = true; database = sql2012Db");
//create command
SqlCommand cmd = new SqlCommand();
try
{
//open connection
conn.Open();
txtSQL.Appendtext("connection opened \n");
txtSQL.Appendtext("command created.\n");
//setting CommandText
cmd.CommandText = @"select Name,productnumber from production.product";
txtSQL.Appendtext("ready to execute SQL statement:\n\t\t\t" + cmd.CommandText);
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message + ex.StackTrace,"exception details");
}
finally
{
conn.Close();
txtSQL.Appendtext("\nconnection closed.");
}
}
然后提示txetsql,在上下文不存在名称,怎么解决