『壹』 如何將textbox的值保存到sql中
給你代碼看看
string connstr = ConfigurationManager.ConnectionStrings["connSQL"].ToString();
SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=資料庫地址;Initial Catalog=資料庫名稱;User ID=資料庫用戶名;Password=密碼;";
SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from [user]"; //SQL命令
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd;
SqlCommandBuilder scb = new SqlCommandBuilder(oda);
DataSet ds = new DataSet();
sda.Fill(ds, "orders");
DataTable table = ds.Tables["orders"];
DataRow row = table.NewRow();
row["資料庫列名稱"] = textbox.text;
table.Rows.Add(row); sda.Update(table);
ds.Tables.Clear();
ds.Dispose();
sda.Dispose();
cmd.Dispose();
conn.Dispose();
這段代碼是要寫到你的.cs文件里
『貳』 如何用C#將textBox數據寫入SQL Server數據表
首先,你的表的列數得滿足你插入的個數;
然後 用SQL語句
還有就是你的這個SQL語句 "insert into goods(水果名稱) values (" + textBox1.Text + ")";goods(水果名稱)表示的是 goods表裡的 「水果名稱」這一列,所以他肯定寫的是不同行。
insert into 表名(列名,列名...) values('"+textBox1.Text +"','"+textBox1.Text +"',....)才是在一行里
希望對你有用
『叄』 c#中,如何把TextBox中的值寫入sql2005資料庫
//這么久了還沒有解決! 還是我貼代碼吧!
//ps: 其實樓上的都是牛人,你提的問題太簡單,大家都不屑回答........
protected void Button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(TextBox1.Text))
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('TextBox1不可為空!')");
return;
}
if (string.IsNullOrEmpty(TextBox2.Text))
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('TextBox2不可為空!')");
return;
}
//連接資料庫字元串
string dbConnString = "server=.;pwd=sa;uid=sa;database=wms";
//不知道你的材料編號是什麽類型,默認為 varchar,r如果是int怎去掉''號就行了
string sql = string.Format("INSERT INTO [wms].[dbo].[ck] (id,NAME) VALUES ('{0}','{1}')", TextBox1.Text.Trim(), TextBox2.Text.Trim());
//可以會發生異常的語句都放在這里
try
{
//using 是系統關鍵字, 作用是自動釋放資源,詳情請網路
using (SqlConnection conn = new SqlConnection(dbConnString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
//打開資料庫連接
conn.Open();
//對數據進行插入操作, 返回影響行數
int val = cmd.ExecuteNonQuery();
//關閉資料庫連接; 用了using 這一句可以省略
conn.Close();
if (val <= 0)
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('插入數據失敗!')");
else
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('插入數據成功!')");
}
}
//捕獲異常
catch (Exception exp)
{
//處理異常.......
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('插入數據失敗! 詳情:" + exp.Message + "')");
}
}
『肆』 Asp中將TextBox控制項的內容插入到sql2005的資料庫中
string str1 = TextBox1.Text;
string str2 = TextBox2.Text;
SqlParameter[] sp = new SqlParameter[]{
new SqlParameter("@name",str1 ),
new SqlParameter("@password",str2 )
};
string sql = "insert bg_class(name,password) values(@name,@password)";
『伍』 在用C#做窗體設計時,怎麼把textbox中的信息添加到sql資料庫的表中
先雙擊注冊按鈕 然後進到後台代碼區,先判斷非空 然後再獲取用戶名 到資料庫去查詢是否存在 要是存在就提示已存在 不存在就執行添加方法。
//這個是驗證非空
public string Validate()
{
if(this.txtUserName.Text.Trim()=="")
{
return "用戶名不能為空";
}else if (this.txtPassWord.Text.Trim()=="")
{
return "密碼不能為空";
}else if(....)
{
........省略了 自己寫。
}else
{
return "";
}
}
//這個是按鈕的事件
protected void btnRegister_Click(object sender, EventArgs e)
{
if(Validate()=="")
{
if(UserNameIsExist(this.txtUserName.Text.Trim()))
{
string sql="insert into Table (UserName,Password,Address,Phone) values(@UserName,@Password,@Address,@Phone)";
List<SqlParameter> paras=new List<SqlParameter>();
paras.Add(new SqlParameter("@UserName",this.txtUserName.Text.Trim()));
paras.Add(new SqlParameter("@Password",this.txtPwd.Text.Trim()));
paras.Add(new SqlParameter("@Address",this.txtAddress.Text.Trim()));
paras.Add(new SqlParameter("@Phone",this.txtPhone.Text.Trim()));
SqlConnection connection=new SqlConnection("server=你的資料庫伺服器名;database=你的資料庫名字;uid=sa;pwd=你的資料庫連接密碼");
SqlCommand cmd=new SqlCommand(sql,connection);
cmd.parameter.AddRange(paras.ToArray());
connection.Open();
if(Convert.ToInt32(cmd.ExecuteNonQuery())==1)
{
MessageBox.Show("注冊成功");
}else
{
MessageBox.Show("注冊失敗");
}
}
else
{
MessageBox.Show("用戶名已經存在!");
}
}
else
{
MessageBox.Show(Validate());
}
}
pubic bool UserNameIsExist(string Name)
{
string sql="select count(*) from Table where UserName="+Name;
SqlConnection connection=new SqlConnection("server=你的資料庫伺服器名;database=你的資料庫名字;uid=sa;pwd=你的資料庫連接密碼");
SqlCommand cmd=new SqlCommand(sql,connection);
connection.Open();
int Count=Convert.ToInt32(cmd.ExcuteScalar());
connection.Close();
return Count>0?false:true;
}
一般情況重置的功能就是將文本框清空的 並不是你說的刪除的功能
你要是真的想做成那樣也是可以的。具體的就是從資料庫查詢你最新添加 的一條信息 然後刪除就OK了 代碼是手打的 有沒有錯誤的 我不是很清楚 但是裡面的有些參數 你自己得看看跟你寫的肯定是不一樣的 所以 你得更改。 這個應該是最詳細的了 要是不給分 那我就白忙活了........
『陸』 textbox的內容插入到sql資料庫代碼在怎麼寫
SqlConnection conn = new SqlConnection(資料庫連接字元串);
conn.open();
SqlCommand cmd = new SqlCommand();
string str = string.Formart("insert into dsyl(title,content) values('{0}','{1}')",this.biaoti.text.toString(),this.neirong.text.toString());
cmd.CommandText = str;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.close;
『柒』 C#怎麼把文本框中的數據寫到SQL資料庫中
private static string connectionStrings = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
寫一個 string sql="insert into table values('"+TextBox1.Text+"')";
調用下面方法,如果返回 1 證明已經添加進去
public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionStrings);
try
{
PrepareCommand(cmd, conn, null, CommandType.Text, safeSql, null);
int result = cmd.ExecuteNonQuery();
return result;
}
catch (Exception e)
{
return 0;
}
finally
{
conn.Close();
conn.Dispose();
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection connection, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (connection == null)
{
connection = new SqlConnection(connectionStrings);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
cmd.Connection = connection;
cmd.CommandText = cmdText;
cmd.CommandTimeout = 50;
if (trans != null)
{
cmd.Transaction = trans;
}
if (cmdType != null)
{
cmd.CommandType = cmdType;
}
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
『捌』 請問怎樣才能把我的textbox1的值放到sql語句裡面再查詢啊,謝謝 在線等
string sql = ("select * from table_1 where name='" + textBox1.Text + "'");
『玖』 c#從textbox空間中添加數據到sql
string sql = "insert into Table_1 (材料,狀態方程,強度模型,失效模型,侵蝕) Values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')";
多了一個「,」。
(材料,狀態方程,強度模型,失效模型,侵蝕) 這裡面的列名和資料庫應該保持一致。
如果資料庫中的欄位數據類型都是char型的就沒錯,如果是其他類型如int,就不需要「『」和「』」。