当前位置:首页 » 编程语言 » textbox1怎么放到sql语言上
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

textbox1怎么放到sql语言上

发布时间: 2022-07-10 02:54:55

‘壹’ 如何将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,就不需要“‘”和“’”。