⑴ 在vs中怎么编写sql的update语句
编写SQL没有区别
你需要连接数据库,执行SQL语句
祝好运,望采纳
⑵ VS2010中,C#,如何用窗体设计器做出SQL语句
您好,这样:
首先在vs2005中引入using System.Data.SqlClient;命名空间
/// <summary>
/// 增加
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public int Insert(string name,string pwd)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "insert into users(name,pwd) values(@name,@pwd)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlParameter parn = new SqlParameter("@name",name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@pwd", pwd);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public int Update(int id)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "delete from users where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter("@id", id);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public int Insert(string name, string pwd,int id)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "update users set name=@name,pwd=@pwd where id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter("@name", name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@pwd", pwd);
cmd.Parameters.Add(parn);
SqlParameter pari = new SqlParameter("@id", id);
cmd.Parameters.Add(pari);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示修改成功
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public DataTable Select()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = "select * from users";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
cmd.Dispose();
return dt;
}
方法写好后,下面举一个查询的例子,在form窗体中拖一个DataGridView,然后在Load方法中
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = Select();
}
这样一运行,DataGridView中就会显示数据了。
⑶ 怎样用VS2008新建一个SQL数据库
点 视图->服务器资源管理器 或直接Ctrl+Alt+S 打开“服务器资源管理器”
在“数据连接”上右键选择“创建新Sql Server 数据库”
然后按提示一步一步来就行了
⑷ 用VS创建一个连接SQL数据库的登陆界面需要哪些步骤最好能把关键代码写出来 感谢
protected void btnQuery_Click(object sender, EventArgs e)
{
string str = "Data Source=.;Initial Catalog=lianxi;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
int count = 0;
try
{
conn.Open();
string sql = "select count (*) from UserForm where UserName=@username and UserPassWord=@userpassword";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(new SqlParameter("@username", this.tbUserName.Text));
cmd.Parameters.Add(new SqlParameter("@userpassword", this.tbUserPassWord.Text));
count = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
if (count > 0)
{
Response.Write("<script>alert('登陆成功!')</script>");
}
else
{
Response.Write("<script>alert('登陆失败!')</script>");
}
}
⑸ 在VS2010中怎样用C#创建数据库连接并执行sql语句 最好举个例子讲一下
用C#联接SQL有两种连接方式,字符串连接和配置文件连接。一个连接字符串的例子是对数据库文件NORTHWEND.MDF的连接
DataSource=.SQLEXPRESS;AttachDbFilename=C:...NORTHWND.MDF;
IntegratedSecurity=True;ConnectTimeout=30;UserInstance=True
数据源的值是.SQLEXPRESS,这里“.”可以写成(local)或者localhost,表示是本机数据库。SQLEXPRESS表示数据库NORTHWEND.MDF是免费产品。由于数据库是文件形式,添加了AttachDbFilename说明。
另外的例子是对于安装在服务器的数据库,例如本机安装的数据库,使用SqlClient连接字符串。连接到AdventureWorks2008的连接字符串示例如下:
DataSource=.;InitialCatalog=AdventureWorks2008;IntegratedSecurity=True
对于SQLServer身份验证,使用指定用户名和密码,这里星号表示有效用户名和密码。
"PersistSecurityInfo=False;UserID=*****;Password=*****;"
+"InitialCatalog=AdventureWorks;Server=MySqlServer"
配置文件是可以按需要更改的XML文件。开发人员可以使用配置文件来更改设置,而不必重编译应用程序。
建议不要在代码中嵌入连接字符串。如果服务器的位置更改,应用程序将需要重新编译。此外,编译成应用程序源代码的未加密连接字符串可以使用MSIL反汇编程序(ildasm.exe)查看而泄密。为了避免将连接字符串存储在代码中,可以将代码存储在ASP.NET应用程序的web.config文件中以及Windows应用程序的app.config文件中。
使用配置文件可以避免记忆连接字符串细节的负担,记忆配置文件的设置过程比记忆连接字符串的细节要容易,因为设置过程按向导进行,智能提示有助于获取连接字符串。下面是VS2010设置配置文件的连接字符串。
具体做法给你推荐一本书:《C#编程指南》,清华大学出版社,2011年1月出版,相关内容有数据库的下载安装、可视化编程、ADO、SQL的FILESTREAM、以及O/R设计器(对象关系设计器)等。在Google或网络输入书名,作者,出版社,有好几家网上书店出售,最低75折,送到家。目前还未在书店上架。
⑹ 怎样用VS编写SQL语句
可以。但是你运行必须在sql server里运行啊。
⑺ vs2010 怎样编写sql查询语句 带textbox的
select * from 用户信息 where 账号 = '" + TextBox1.Text + "'//字段按着这个改吧
⑻ 如何用vs2005创建SQL Server 2005数据库
是用代码还是集成环境?
1.如果是用代码的话,就需要调用对应的类去执行SQL语句创建数据库就可以了,
2.如果是用集成环境的话,里面就有数据库的选项卡,连接到服务器去创建SQL Server 2005数据库就可以了,
呵呵,希望能有帮助,^_^
⑼ vs2008中如何利用sql语句
1.创建SqlConnection的实例
2.创建SqlDataAdapter的实例,如果需要,可根据select语句生成其他SQL语句
3.创建DataSet的实例
4.使用Fill方法将数据库中的表填充到DataSet的表中
5.利用DataGridView或者其他控件编辑或显示数据
6.根据需要,使用Update方法跟新数据库
string connectionString="Integrated Security=SSPI;Database=MyDatabase.mdf;Server=localhost;";
SqlConnection conn=newSqlConnection(connectionString);
SqlDataAdapter adapter=new SqlDataAdapter("SELECT * FROM MyTable1",conn);
DataSet dataset=new DataSet();
adapter.Fill(dataset);
dataGridView1.DataSource=dataset.Table[0];