⑴ 在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];