基础呀,
string newtablecmd;
int cmdresults;
newtablecmd = "CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1) PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)";// The following two property settings can also be done
// in the Properties window, but are shown here for completeness.
OleDbCommand1.CommandType = CommandType.Text;
OleDbCommand1.CommandText = newtablecmd;// The connection must be open before you can execute a command.
OleDbConnection1.Open();
cmdresults = OleDbCommand1.ExecuteNonQuery();
OleDbConnection1.Close();
MessageBox.Show("After creating the table, results = " + cmdresults.ToString());
string newtablecmd;
int cmdresults;
newtablecmd = "CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1) PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)";// The following two property settings can also be done
// in the Properties window, but are shown here for completeness.
OleDbCommand1.CommandType = CommandType.Text;
OleDbCommand1.CommandText = newtablecmd;// The connection must be open before you can execute a command.
OleDbConnection1.Open();
cmdresults = OleDbCommand1.ExecuteNonQuery();
OleDbConnection1.Close();
MessageBox.Show("After creating the table, results = " + cmdresults.ToString());
解决方案 »
- 截取字符串
- 有比visual assist x还要好用的VS插件吗?
- 请教高手,C#winfrm编程使用水晶报表打印的一个问题
- socket服务器
- 关于继承的小问题
- 谁有.net的hibernate设计资料,高分寻求!
- 菜鸟求助!!!!!!
- 如何将dateset里面的数据导入到sql server中?
- 请大家帮忙,要用C#做个小工具,把EXCEL文档导入,然后转换成UTF-8编码的txt文档,怎么做啊?
- 如何在web页面使用 Windows 窗体控件,最好有例子!
- 在winform里如何实现一个报表设计器,类似于vb的报表设计器?
- 怎样将一个DataTable中指定的几条记录复制到一个新Table里?
最基础的东西
SqlConnection conn = new SqlConnection(conectStr);//建立连接
string select = "select regiondescription from region where regionid = 1"
SqlCommand cm = new SqlCommand(select,conn);//建立一个查询命令
conn.open();//打开连接
string result = cm.sCommand.ExecuteScalar().ToString();//取回结果,放在result中。
conn.Close();//关闭连接
其他的操作都一样,把select的内容换成你想要的sql命令
然后注意cm的执行方法。如果是删除,或者更新,cm.ExecuteNonQuery();
建议楼主看Msdn中的System.Data.SqlClient命名空间
string str = "user id=sa;password=;initial catalog=test;data source=MZW;Connect Timeout=30";
SqlConnection conn = new SqlConnection(str);
//付给dataset控件
string select = "select * from xsxx";
DataSet ds = new DataSet();
SqlDataAdapter cmm = new SqlDataAdapter(select,conn);
cmm.Fill(ds,"xsxx");
//绑定DataGrid
DataGrid1.DataSource = ds.Tables["xsxx"].DefaultView;
DataGrid1.DataBind();
string str = "user id=sa;password=;initial catalog=test;data source=MZW;Connect Timeout=30";
SqlConnection conn = new SqlConnection(str);
//执行command
string select = "select * from xsxx";
SqlCommand cmd= new SqlCommand(select,conn);
cmd.Connection.Open();
//付给SqlDataReader
SqlDataReader myReader;
myReader = cmd.ExecuteReader();//连接数据库方法
//在web.config文件中加入下列代码
<appSettings>
<add key="contodata" value="user id=sa;password=;initial catalog=test;data source=MZW;Connect Timeout=30"/>
</appSettings>
//连接数据库,并且使用名字空间 using System.Configuration;
string strconn = ConfigurationSettings.AppSettings["contodata"];
SqlConnection conn = new SqlConnection(strconn);
delete from 表名 where 列名 = '你的条件'
update 表名 set 列名 = '你的值' where 列名 = '你的条件'
insert into 表名(列名1,列名2,...,列名n) value(值1,值2,...,值n)
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"customers");
conn.Open();
this.sqlInsertCommand1.CommandText = @"INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)";
this.sqlInsertCommand1.Connection = conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address, City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax WHERE (CustomerID = @Original_CustomerID)";
this.sqlUpdateCommand1.Connection = conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = @"DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) ";
this.sqlDeleteCommand1.Connection = conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
SqlCommand thisCommand=thisConnection.CreateCommand();thisCommand.CommandText="Select ID,username from manage";
SqlDataReader thisReader=thisCommand.ExecuteReader();
thisReader=thisCommand.ExecuteReader();
while(thisReader.Read())
{
textBox1.Text+=thisReader["ID"]+" "+thisReader["UserName"]+"\r\n";
}thisReader.Close();thisConnection.Close();
这是一段调用数据库的代码,你可以看到在第一行有一个参数和SQL语句是一样的,就在那个地方。