【发布时间】:2016-06-02 10:14:06
【问题描述】:
SqlConnection con = new SqlConnection(@"server=MOON\SQLEXPRESS;Initial Catalog=Moon;Integrated Security=True");
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.btnsubmit);
button.Click += button_Click;
}
void button_Click(object sender, EventArgs e)
{
try
{
EditText etuser = FindViewById<EditText>(Resource.Id.txtuser);
EditText etpass = FindViewById<EditText>(Resource.Id.txtpass);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Login values('" + etuser.Text + "','" + etpass.Text + "')", con);
cmd.ExecuteReader();
con.Close();
}
catch(Exception ex)
{
string result;
result = ex.Message;
Toast.MakeText(this, result, ToastLength.Short).Show();
}
}
请帮帮我,我想插入一条记录,但出现错误
无法解析主机
MOON
请帮帮我。我正在使用本地数据库,我的服务器名称也是MOON\SQLExpress。
【问题讨论】:
-
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入
-
您需要为您的服务器指定一个 IP 或完全限定的域名。另请注意,从您的移动应用程序直接连接到数据库服务器是一个可怕的想法。您通常应该使用 Web 服务层来代替。
-
直接从您的移动应用程序连接到您的数据库不是一个好主意。创建一些将提供所需功能的基本 Web API。或者为移动应用程序使用一些云 API。除非您正在为移动设备开发 sql 管理工作室。
-
非常感谢您的帮助
标签: sql-server sql-server-2008 xamarin xamarin.android