运行连接到SQL DB的网页时出错

运行连接到SQL DB的网页时出错

问题描述:

大家好,

我是编程世界的新手.尝试使用WPF将C#与sql db连接时遇到困难.

我想做什么?

运行一个网页,该网页会将网页中的信息插入db(客户信息)表中.
我使用了文本块来获取信息并添加到DataRow中.然后将DataRow插入到Dataset中,最后通过SqlDataAdapter更新数据库表.

源代码:

Hi guys,

I''m new to programming world. I am facing difficulty when trying to connect C# with sql db using WPF.

What I was trying to do?

Run a webpage which will insert the information in the webpage to the table in db(customer info).
I used the textblock to get the info(s) and add to the DataRow. Then insert the DataRow to the Dataset and finally update db table through SqlDataAdapter.

Source Code:

public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
        private void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
                SqlConnection conn = new SqlConnection();
                
                string connString = "server=gladiator\\sqlexpress; database=MoviesStorage; Integrated Security=SSPI; ";
                conn.ConnectionString = connString;
                conn.Open();

                string commString = "SELECT * FROM Customer_Info";
                SqlCommand comm = new SqlCommand(commString, conn);
                SqlDataAdapter dtAdapter = new SqlDataAdapter(comm);
                SqlCommandBuilder commBuilder = new SqlCommandBuilder(dtAdapter);

                DataSet dtSet = new DataSet();
                dtAdapter.Fill(dtSet, "Customer_Info");

                DataRow dtRow = dtSet.Tables["Customer_Info"].NewRow();
                dtRow["myID"] = textID.Text;
                dtRow["myName"] = textName.Text;
                dtRow["myIC"] = textIC.Text;
                dtRow["myContact"] = textContact.Text;
                dtSet.Tables["Customer_Info"].Rows.Add(dtRow);

                dtAdapter.Update(dtSet, "Customer_Info");
                conn.Close();
        }    
}


编译器退出时没有错误.但是一旦执行,就会抛出一条我不理解的异常消息.它发生在conn.Open()行中.

引发异常:
未知模块中发生了类型为"System.Security.SecurityException"的未处理的异常."


有人能帮助我吗?与该问题有关的任何其他链接也将受到赞赏.

谢谢,
Skunhhead


The compiler exits without erros. But once I execute, there is an exception message thrown which I do not understand. It happens in line conn.Open().

Exception thrown:
"An unhandled exception of type ''System.Security.SecurityException'' occurred in Unknown Module."


Does anyone able to help me on this? Any other link relating to this problem are also appreciated.

Thanks,
Skunhhead

使用连接字符串:

Use connection string:

string connString = "server=gladiator\\sqlexpress; database=MoviesStorage; Integrated Security=True;"; //Or
//string connString = "server=gladiator\\sqlexpress; database=MoviesStorage;uid=" + username + ";pwd=" + password + ";";  



请查看此链接,也许对您有帮助:
http://www.secnewsgroups.net/dotnet/t1385-security-exception-related- network.aspx [ ^ ]



Please view this link, maybe it helps you:
http://www.secnewsgroups.net/dotnet/t1385-security-exception-related-network.aspx[^]


这通常与代码访问安全性有关.看看这些讨论:
链接1 [ ^ ]
链接2 [链接3 [
This is generally related to Code Access Security. Have a look at these discussions:
Link 1[^]
Link 2[^]
Link 3[^]


感谢大家的答复,我找到了解决此问题的方法.

看看下面的链接:
WPF数据网格简介 [
谢谢.
Thanks guys for your replies, and i found the fix this problem.

Have a look at the below link:
Introduction to the WPF DataGrid[^]

Go down the page to the "Comments and Discussion".

Thx.