我想使用ssms插入数据,但不希望使用.net core插入ssms中的数据?
插入代码WebForm1.aspx.cs:
namespace InsertUpdateDelete.scripts
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void btninsert_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string address = txtAddress.Text;
Insert_Click(name, address);
}
void Insert_Click(string name, string address)
{
SqlConnection con = new SqlConnection(connstring);
string query = "Insert into DemoInUpDelete (Name, Address) values (@Name,@Address)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
//Pass values to Parameters
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Address", address);
try
{
con.Open();
int validateOperation = cmd.ExecuteNonQuery();
if (validateOperation > 0)
{
//Message insert succesfully
}
else
{
//Error
}
}
catch (SqlException e)
{
//Exception
}
finally
{
con.Close();
}
void Update_Click(object sender, EventArgs e)
{
//update
}
void Delete_Click(object sender, EventArgs e)
{
//delete
}
}
}
WebForm.aspx:
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Address:</td>
<td><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td>
<asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="Update_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Delete" OnClick="Delete_Click" />
</td>
</tr>
</table>
</form>
</body>
我在新的.net中如何在sql server中插入数据SQL Server Management Studio? 我正在创建一个简单的new-> project但未插入数据? 数据库: 我正在创建数据库和ID(主键)名称(varchar(20)地址(varchar(20)
.net输入错误
SqlConnection con =新的SqlConnection(connstring); connstring不 存在当前上下文
web.config.cs:
<configuration>
<connectionStrings>
<add name="ConnStringName" connectionString="Data Source= DESKTOP-U3PB1TF\SA; Integrated Security=true;Initial Catalog= InUpDelete; uid=sa; Password=admin@123; " providerName="System.Data.SqlClient" />
</connectionStrings>
</connectionStrings>
答案 0 :(得分:3)
@Jeremy Thompson声明为注释,您错过了Insert操作代码
下面的代码可能会对您有所帮助。
aspx代码:
<asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
C#代码:
protected void btninsert_Click(object sender,EventArgs e)
{
string name=txtName.Text;
string address=txtAddtess.Text;
Insert_Click(name,address);
}
void Insert_Click(string name,string address)
{
string connstring=System.Configuration.ConfigurationManager.
ConnectionStrings["ConnStringName"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);
string query = "Insert into DemoInUpDelete (Name, Address) values (@Name,@Address)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType=CommandType.Text;
//Pass values to Parameters
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Price", address);
try
{
con.Open();
int validateOperation=cmd.ExecuteNonQuery();
if(validateOperation>0)
{
//Message insert succesfully
}
else
{
//Error
}
}
catch (SqlException e)
{
//Exception
}
finally
{
con.Close();
}
}